import 'dart:convert';

import 'package:dk_flutter_base/dk_core.dart';
import 'package:dk_flutter_base/dk_ui.dart';
import 'package:dk_flutter_module_sale/common/config/page_base_common.dart';
import 'package:dk_flutter_module_sale/config/route/common_widget_route.dart';

@ARoute(url: CommonWidgetRoute.COMMON_TEST)
class TestPage extends CommonScaffoldPage {
  final BoostRouteOption option;

  TestPage(this.option) : super();

  @override
  DkAppBar getAppBar(CommonPageBloc pageBloc) {
    return DkAppBar(
      DKAppBarParams.withBackNoMenu(
        pageBloc.context,
        titleContent: "Flutter内部界面跳转",
      ),
    );
  }

  @override
  DkScaffoldOption configDefaultScaffoldOption(DkScaffoldOption option) {
    return option..backgroundColor = Colors.white;
  }

  @override
  void onInitState(CommonPageBloc pageBloc) {
    pageBloc.switchPage(TestWidget(option.params));
  }
}

class TestWidget extends StatefulWidget {
  final Map localParams;

  TestWidget(this.localParams);

  @override
  State<TestWidget> createState() {
    return TestWidgetState();
  }
}

class TestWidgetState extends State<TestWidget> {
  TextEditingController routeController;
  TextEditingController paramController;
  WidgetRouteBean widgetRouteBean;
  bool isShowRouteDelete;
  bool isShowParamDelete;

  @override
  void initState() {
    super.initState();
    routeController = TextEditingController.fromValue(
      TextEditingValue(
        text: "dkwidget://app/",
        // 保持光标在最后
        selection: TextSelection.fromPosition(TextPosition(
            affinity: TextAffinity.downstream,
            offset: "dkwidget://app/".length)),
      ),
    );
    paramController = TextEditingController();

    if (mapNotEmpty(widget.localParams)) {
      widgetRouteBean = WidgetRouteBean.fromJson(widget.localParams);
    }
    if (objNotEmpty(widgetRouteBean)) {
      if (stringNotEmpty(widgetRouteBean.widgetRoute)) {
        routeController.text = dkDecodeBase64(widgetRouteBean.widgetRoute);
      }

      if (stringNotEmpty(widgetRouteBean.widgetParam)) {
        routeController.text = dkDecodeBase64(widgetRouteBean.widgetParam);
      }
    }

    isShowRouteDelete = stringNotEmpty(widgetRouteBean?.widgetRoute);
    isShowParamDelete = stringNotEmpty(widgetRouteBean?.widgetParam);
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Padding(
          padding: EdgeInsets.all(30),
          child: Row(
            children: <Widget>[
              Expanded(
                child: DkTextField(
                  DkTextFieldOption(
                    controller: routeController,
                    decoration: InputDecoration(
                        hintText: "路由地址：dkwidget://app开头",
                        hintStyle: TextStyle(
                          color: Color(0xff999999),
                          fontSize: 14,
                        )),
                    style: TextStyle(color: Color(0xff333333), fontSize: 14),
                    onChanged: (String content) {
                      if (stringBlank(content)) {
                        if (isShowRouteDelete) {
                          setState(() {
                            isShowRouteDelete = false;
                          });
                        }
                      } else {
                        if (!isShowRouteDelete) {
                          setState(() {
                            isShowRouteDelete = true;
                          });
                        }
                      }
                    },
                  ),
                ),
              ),
              DkVisibilityCompose(
                VisibilityComposeOption.gone((BuildContext context) {
                  return IconButton(
                      alignment: Alignment.centerRight,
                      padding: EdgeInsets.only(right: 7.5),
                      icon: Icon(
                        Icons.cancel,
                        color: Color(0xffBFBFC2),
                        size: 15,
                      ),
                      onPressed: () {
                        setState(() {
                          isShowRouteDelete = false;
                          routeController.text = "";
                        });
                      });
                }, visible: isShowRouteDelete),
              ),
            ],
          ),
        ),
        Padding(
          padding: EdgeInsets.only(left: 30, right: 30),
          child: Row(
            children: <Widget>[
              Expanded(
                child: DkTextField(
                  DkTextFieldOption(
                    controller: paramController,
                    decoration: InputDecoration(
                        hintText: "界面参数：json格式",
                        hintStyle: TextStyle(
                          color: Color(0xff999999),
                          fontSize: 14,
                        )),
                    style: TextStyle(color: Color(0xff333333), fontSize: 14),
                    onChanged: (String content) {
                      if (stringBlank(content)) {
                        if (isShowParamDelete) {
                          setState(() {
                            isShowParamDelete = false;
                          });
                        }
                      } else {
                        if (!isShowParamDelete) {
                          setState(() {
                            isShowParamDelete = true;
                          });
                        }
                      }
                    },
                  ),
                ),
              ),
              DkVisibilityCompose(
                VisibilityComposeOption.gone((BuildContext context) {
                  return IconButton(
                      alignment: Alignment.centerRight,
                      padding: EdgeInsets.only(right: 7.5),
                      icon: Icon(
                        Icons.cancel,
                        color: Color(0xffBFBFC2),
                        size: 15,
                      ),
                      onPressed: () {
                        setState(() {
                          isShowParamDelete = false;
                          paramController.text = "";
                        });
                      });
                }, visible: isShowParamDelete),
              ),
            ],
          ),
        ),
        Padding(
          padding: EdgeInsets.only(top: 30),
          child: DKButton(
            DKButtonOption(
              child: DkText.simple("跳转", 17, Colors.white),
              color: Color(0xFF3DBCC6),
              onPressed: () {
                String route = routeController.text;
                if (stringBlank(route)) {
                  dkShowToast("请输入路由地址");
                  return;
                }
                Map<String, dynamic> routeParams;
                if (stringNotEmpty(paramController.text)) {
                  routeParams = json.decode(paramController.text);
                }
                RouteUtil.goPage(route,
                    widgetParams: routeParams, widgetContext: context);
              },
            ),
          ),
        ),
      ],
    );
  }
}

class WidgetRouteBean {
  String widgetRoute;
  String widgetParam;

  WidgetRouteBean({this.widgetRoute, this.widgetParam});

  WidgetRouteBean.fromJson(Map<String, dynamic> json) {
    this.widgetRoute = json['widgetRoute'];
    this.widgetParam = json['widgetParam'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['widgetRoute'] = this.widgetRoute;
    data['widgetParam'] = this.widgetParam;
    return data;
  }
}
